package cx.com.diy.adapter;

import java.util.ArrayList;

import xc.com.ui.MainActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ViewPagerAdapter extends FragmentStatePagerAdapter implements
		OnPageChangeListener {
	private MainActivity mContext;
	RadioButton  button,button1;
	private RadioGroup rgs;
	private ViewPager mViewPager = null;
	private ArrayList<TabInfo> mTabList = new ArrayList<TabInfo>();

	public ViewPagerAdapter(FragmentManager fm) {
		super(fm);
		// TODO Auto-generated constructor stub
	}

	public ViewPagerAdapter(MainActivity mainActivity, ViewPager viewPager,
			RadioGroup rgs) {
		// TODO Auto-generated constructor stub
		super(mainActivity.getSupportFragmentManager());
		mContext = mainActivity;
		this.rgs = rgs;
		mViewPager = viewPager;
		mViewPager.setAdapter(this);
		mViewPager.setOnPageChangeListener(this);
	}

	// private ArrayList<TabInfo> mTabList = new ArrayList<TabInfo>();
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		

	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		
		button=(RadioButton)rgs.getChildAt(position);

		for(int i=0;i<rgs.getChildCount();i++){
			
			button1=(RadioButton)rgs.getChildAt(i);
			if(button1.isChecked()&&i!=position){
				button1.setChecked(false);
			}
		}
		button.setChecked(true );
	}

	@Override
	public android.support.v4.app.Fragment getItem(int position) {
		// TODO Auto-generated method stub
		TabInfo tab = mTabList.get(position);
		if (tab.fragment == null) {
			tab.fragment = android.support.v4.app.Fragment.instantiate(
					mContext, tab.clazz.getName(), tab.bundle);
		}

		return tab.fragment;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mTabList.size();
	}

	public void add(Class<?> clazz, Bundle bundle) {
		// TODO Auto-generated method stub
		TabInfo tabInfo = new TabInfo(clazz, bundle);

		mTabList.add(tabInfo);
		notifyDataSetChanged();
	}

	public final class TabInfo {
		private final Class<?> clazz;
		private final Bundle bundle;
		android.support.v4.app.Fragment fragment;

		TabInfo(Class<?> clazz, Bundle bundle) {
			this.clazz = clazz;
			this.bundle = bundle;
		}
	}
}
